package com.zenith.user.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.user.po.dto.LoginDTO;
import com.zenith.user.po.dto.RegisterDTO;
import com.zenith.user.po.vo.LoginVO;
import com.zenith.user.service.AnthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/user/anth")
@Tag(name = "用户认证")
public class AnthController {
    @Autowired
    private AnthService anthService;

    /**
     * 用户登录接口
     */
    @SecurePostMapping(value = "/login", roles = {"USER"}, permission = "user:login")
    @Operation(summary = "用户登录")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        return Result.success(anthService.login(loginDTO));
    }

    /**
     * 用户注册接口
     */
    @SecurePostMapping(value = "/register", roles = {"USER"}, permission = "user:register")
    @Operation(summary = "用户注册")
    public Result<Object> register(@RequestBody RegisterDTO registerDTO) {
        anthService.register(registerDTO);
        return Result.success("注册成功");
    }

    /**
     * 用户登出接口
     */
    @SecurePostMapping(value = "/logout", roles = {"USER"}, permission = "user:logout")
    @Operation(summary = "用户登出")
    public Result<Object> logout(@RequestHeader("Authorization") String token) {
        anthService.logout(token);
        return Result.success("登出成功");
    }

    /**
     * 检查用户是否是活跃状态
     */
    @SecurePostMapping(value = "/getUserActive", roles = {"USER"}, permission = "user:getUserActive")
    @Operation(summary = "检查用户是否是活跃状态")
    public Result<Boolean> getUserActive(@RequestParam("userId") Long userId) {
        return Result.success(anthService.getUserActive(userId));
    }

    /**
     * 获取当前用户信息接口
     */
    @SecurePostMapping(value = "/getCurrentUser", roles = {"USER"}, permission = "user:getCurrentUser")
    @Operation(summary = "获取当前用户信息")
    public Result<Object> getCurrentUser(@RequestParam("userId") Long userId) {
        return Result.success(anthService.getCurrentUser(userId));
    }

    /**
     * 刷新Token接口
     */
    @SecurePostMapping(value = "/refreshToken", roles = {"USER"}, permission = "user:refresh")
    @Operation(summary = "刷新Token")
    public Result<Object> refreshToken(@RequestHeader("Authorization") String token) {
        return Result.success(anthService.refreshToken(token));
    }
}
